
Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Web
Imports System.Web.SessionState
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls
Imports _2bMatesSite.BO

Namespace _2bMatesSite
	''' <summary>
	''' Summary description for send_blog.
	''' </summary>
	Public Partial Class send_blog
		Inherits BaseWebPage
		Protected lblPostBlog As System.Web.UI.WebControls.Label
		'protected System.Web.UI.WebControls.HyperLink hlUMGroupBulletin;
		Protected hlGroupBulletin As System.Web.UI.WebControls.HyperLink
		Protected hlGroupTrash As System.Web.UI.WebControls.HyperLink
		Protected hlGroupBlog As System.Web.UI.WebControls.HyperLink
		'protected System.Web.UI.WebControls.HyperLink hlReturnToBlogList;
		Protected trGroupMenu As System.Web.UI.HtmlControls.HtmlTableRow
		Protected sLinkTag As String = Constants.BLOG_ADD_LINK_TAG
		Protected sImageTag As String = Constants.BLOG_ADD_IMAGE_TAG
		Protected btnAddLink As System.Web.UI.WebControls.Button
		Protected btnAddImage As System.Web.UI.WebControls.Button
		Protected EditBlogID As Integer = -1
		Protected isGroupMode As Boolean = False
		Protected isModerator As Boolean = False
		Protected grpID As Integer = -1
		Protected grp As Group = Nothing
		Protected usr As User = Nothing
		Protected htPageConstants As New Hashtable()

		Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
			' Put user code to initialize the page here
			' this.pageTemplate.PageTitle = PageTitles.SEND_BLOG_PAGE;

			usr = LoggedUser
			If usr = Nothing Then
				Common.Utils.RedirectToLoginPage(Page)
			End If

			' init page from XML
			Me.initPageTitles(Constants.SEND_BLOG, "", Form1, Nothing, htPageConstants)
			Me.initAdvertisements(Form1, Nothing)

			' init tabs
			ucBlogTabs.ActiveTab = 2
			ucBlogTabs.PageName = Constants.SEND_BLOG
			grpBlogTabs.ActiveTab = 2
			grpBlogTabs.PageName = Constants.SEND_BLOG

			' check if a group param is given  
			If Request.Params(Constants.GROUP_PARAM) <> Nothing AndAlso Request.Params(Constants.GROUP_PARAM) <> "" Then
				isGroupMode = True

				Try
					grpID = Convert.ToInt32(Request.Params(Constants.GROUP_PARAM))
				Catch
					Throw New Exception(Constants.ERROR_MESSAGE_MISSING_PARAMETERS)
				End Try

				grp = Groups.GetGroupByID(grpID)
				If grp = Nothing Then
					Throw New Exception(Constants.ERROR_MESSAGE_GROUP_DOESNT_EXIST_ERROR)
				End If

				If Not grp.Members.Contains(usr.ID) Then
					Throw New Exception(Constants.ERROR_MESSAGE_ONLY_MEMBER_CAN_VIEW_MESSAGES)
				End If

				' set moderator mode
				isModerator = (grp.Uid = usr.ID)

				If Not isModerator Then
					Throw New Exception(Constants.ERROR_MESSAGE_ONLY_MODERATOR_CAN_POST_BLOG)
				End If

				' set menu type to group
				litMainPageTitle.Text = Me.GetPageConstText(htPageConstants, "CONST_GroupTitle_TEXT")
				ucBlogTabs.Visible = False

					' set return hyperlink for a group
					'hlReturnToBlogList.NavigateUrl = "../" + Constants.BLOG + "?" + Constants.GROUP_PARAM + "=" + grp.ID;
					'hlReturnToBlogList.Text = "Return to group blog list";
					'hlReturnToBlogList.Text = this.GetPageConstText(htPageConstants, "CONST_ReturnToGroupBlog_TEXT");
					'btnPost.Text = this.GetPageConstText(htPageConstants, "CONST_PostGroupBulletin_TEXT");
				grpBlogTabs.Visible = True
			End If

			' init blog if in edit mode
			If Request.Params("id") <> Nothing AndAlso Request.Params("id") <> Nothing Then

				Try
					EditBlogID = Convert.ToInt32(Request.Params("id"))
				Catch
					Throw New Exception(Constants.ERROR_MESSAGE_MISSING_PARAMETERS)
				End Try

				If btn_send_bulletin.CommandName <> "Edit" Then

					' init send button
					btn_send_bulletin.CommandName = "Edit"
					btn_send_bulletin.CommandArgument = EditBlogID.ToString()
					btn_send_bulletin.Text = Me.GetPageConstText(htPageConstants, "CONST_SubmitChanges_TEXT")
					btn_send_bulletin.Width = 100

					' init content of the blog
					Dim grpBlog As GroupBlog = Nothing
					Dim blog As Blog = Nothing

					If isGroupMode Then
						grpBlog = Messages.GetGroupBlog(grp.ID, EditBlogID)
						txtSubject_bulletin.Text = grpBlog.Subject
						txtMessage_bulletin.Text = grpBlog.Body
					Else
						blog = Messages.GetUserBlog(LoggedUser.ID, EditBlogID)
						txtSubject_bulletin.Text = blog.Subject
						txtMessage_bulletin.Text = blog.Body
					End If
				End If
			Else
				btn_send_bulletin.CommandName = "Send"
			End If

			If isGroupMode Then
				ucBlogTabs.Visible = False
				grpBlogTabs.Visible = True

				hlRightLink.Text = (Me.GetPageConstText(htPageConstants, "CONST_GroupBackLink_TEXT")).Replace("[PARAM]", grp.Name)
				hlRightLink.NavigateUrl = "../" + Constants.GROUP + "?" + Constants.GROUP_PARAM + "=" + grp.ID
			Else
				grpBlogTabs.Visible = False

				ucBlogTabs.Visible = True
			End If

			txtMessage_bulletin.Attributes.Add("onKeyDown", Constants.ON_KEY_DOWN_SCRIPT.Replace(Constants.TEXT_AREA_MAX_CHARS_TAG, Constants.TEXT_AREA_BLOG_MAX_CHARS.ToString()))
			txtMessage_bulletin.Attributes.Add("onKeyUp", Constants.ON_KEY_UP_SCRIPT.Replace(Constants.TEXT_AREA_MAX_CHARS_TAG, Constants.TEXT_AREA_BLOG_MAX_CHARS.ToString()))
		End Sub

		#region Web Form Designer generated code
		Protected Overloads Overrides Sub OnInit(ByVal e As EventArgs)
			'
			' CODEGEN: This call is required by the ASP.NET Web Form Designer.
			'
			InitializeComponent()
			MyBase.OnInit(e)
		End Sub

		''' <summary>
		''' Required method for Designer support - do not modify
		''' the contents of this method with the code editor.
		''' </summary>
		Private Sub InitializeComponent()
			AddHandler Me.cvHtmlInSubject.ServerValidate, AddressOf Me.cvHtmlInSubject_ServerValidate
			AddHandler Me.cvHtmlInBody.ServerValidate, AddressOf Me.cvHtmlInBody_ServerValidate
		End Sub
#End Region

		Protected Sub btn_send_bulletin_Click(ByVal sender As Object, ByVal e As System.EventArgs)

			If Page.IsValid Then
				Dim subject As String = ""
				Dim body As String = ""

				subject = txtSubject_bulletin.Text.Trim()
				body = txtMessage_bulletin.Text.Trim()

				subject = removeScriptTags(subject.Substring(0, Math.Min(subject.Length, Constants.TEXT_FIELD_DEFAULT_MAX_CHARS)))
				body = initBlogMessage(removeScriptTags(body.Substring(0, Math.Min(body.Length, Constants.TEXT_AREA_LONG_MESSAGES_MAX_CHARS))))

				litSubjectConfirm.Text = String.Format("<span class='main_text_opposite_bold'>{0}:&nbsp;</span>{1}", "Subject", subject)
				litBodyConfirm.Text = String.Format("<span class='main_text_opposite_bold'>{0}:&nbsp;</span>{1}", "Body", body)

				trSendBlog.Visible = False
				trConfrimation.Visible = True
			End If

			'	string cmd = btn_send_bulletin.CommandName;
'			if ( Page.IsValid )
'			{
'				switch (cmd)
'				{
'					case "Send":
'						DoSendBlog();
'						break;
'					case "Edit":
'						DoEditBlog();
'						break;
'				}
'			}
'		

		End Sub

		Protected Sub btnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs)
			If isGroupMode Then
				Response.Redirect(Constants.BLOG + "?" + Constants.GROUP_PARAM + "=" + grp.ID)
			Else
				Response.Redirect(Constants.BLOG)
			End If
		End Sub

		Protected Sub btnPost_Click(ByVal sender As Object, ByVal e As System.EventArgs)

			Dim cmd As String = btn_send_bulletin.CommandName

			Select Case cmd
				Case "Send"
					DoSendBlog()
					Exit Select
				Case "Edit"
					DoEditBlog()
					Exit Select
			End Select

			'			string subject = "";
'			string body = "";
'
'			subject = txtSubject_bulletin.Text.Trim();
'			body = txtMessage_bulletin.Text.Trim();
'
'			subject = removeScriptTags(subject.Substring(0, Math.Min(subject.Length, Constants.TEXT_FIELD_DEFAULT_MAX_CHARS)));
'			body = removeScriptTags(body.Substring(0, Math.Min(body.Length, Constants.TEXT_AREA_LONG_MESSAGES_MAX_CHARS)));
'
'			if ( isGroupMode )
'			{
'				GroupBulletinMessage bltNew = new GroupBulletinMessage( LoggedUser.ID, grpID, subject, body );
'				bltNew.Send();
'				Response.Redirect( Constants.BULLETIN + "?" + Constants.GROUP_PARAM + "=" + grp.ID );
'			}
'			else
'			{
'				BulletinMessage bltNew = new BulletinMessage( LoggedUser.ID, subject, body );
'				bltNew.Send();
'				Response.Redirect( Constants.BULLETIN );
'			}
'
		End Sub

		Protected Sub btnEdit_Click(ByVal sender As Object, ByVal e As System.EventArgs)
			trSendBlog.Visible = True
			trConfrimation.Visible = False
		End Sub

		Private Sub cvHtmlInSubject_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs)
			Dim fn As String = Request.Form(txtSubject_bulletin.UniqueID)

			If isHTML(fn) Then
				args.IsValid = False
			Else
				args.IsValid = True
			End If
		End Sub

		Private Sub cvHtmlInBody_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs)
			'
'			 string fn = Request.Form[txtMessage_bulletin.UniqueID];
'
'			if (isHTML(fn))
'				args.IsValid = false;
'			else
'				args.IsValid = true;
'			

			args.IsValid = True
		End Sub

		Private Sub DoSendBlog()
			' create new blog
			Dim subject As String = ""
			Dim body As String = ""

			subject = txtSubject_bulletin.Text.Trim()
			body = txtMessage_bulletin.Text.Trim()

			subject = subject.Substring(0, Math.Min(subject.Length, Constants.TEXT_FIELD_DEFAULT_MAX_CHARS))
			body = body.Substring(0, Math.Min(body.Length, Constants.TEXT_AREA_MESSAGES_MAX_CHARS))

			If isGroupMode Then
				' send group blog
				Dim grpBlog As New GroupBlog(grp.ID, LoggedUser.ID, subject, body)
				grpBlog.Send()
				Response.Redirect(Constants.BLOG + "?" + Constants.GROUP_PARAM + "=" + grp.ID)
			Else
				Dim blogNew As New Blog(LoggedUser.ID, subject, body)
				blogNew.Send()
				Response.Redirect(Constants.BLOG)
			End If
		End Sub

		Private Sub DoEditBlog()
			Dim subject As String = ""
			Dim body As String = ""

			subject = txtSubject_bulletin.Text.Trim()
			body = txtMessage_bulletin.Text.Trim()

			subject = subject.Substring(0, Math.Min(subject.Length, Constants.TEXT_FIELD_DEFAULT_MAX_CHARS))
			body = body.Substring(0, Math.Min(body.Length, Constants.TEXT_AREA_BLOG_MAX_CHARS))

			If isGroupMode Then
				' save group blog
				Dim grpBlog As GroupBlog = Messages.GetGroupBlog(grp.ID, EditBlogID)
				grpBlog.Subject = subject
				grpBlog.Body = body
				grpBlog.Save()
				Response.Redirect(Constants.BLOG + "?" + Constants.GROUP_PARAM + "=" + grp.ID)
			Else
				Dim blog As Blog = Messages.GetUserBlog(LoggedUser.ID, EditBlogID)
				blog.Subject = subject
				blog.Body = body
				blog.Save()
				Response.Redirect(Constants.BLOG)
			End If
		End Sub

	End Class
End Namespace






